package com.doradance.app.mine.collect;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.entity.NullCollectEntity;
import com.doradance.entity.SelectCallBack;
import com.example.xlhratingbar_lib.XLHRatingBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by guohao on 2017/9/6.
 */

public class MineRadioAdapter extends RecyclerView.Adapter<MineRadioAdapter.ViewHolder> {

    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;

    private int secret = 0;
    private String title = "";
    private Context context;
    private List<MyLiveList> mMyLiveList;
    private OnItemClickListener mOnItemClickListener;
    private OnItemLongClickListener mOnItemLongClickListener;
    public MineRadioAdapter(Context context) {
        this.context = context;
    }


    public void notifyAdapter(List<MyLiveList> myLiveList, boolean isAdd) {
        if (!isAdd) {
            this.mMyLiveList = myLiveList;
        } else {
            this.mMyLiveList.addAll(myLiveList);
        }
        notifyDataSetChanged();
    }

    public List<MyLiveList> getMyLiveList() {
        if (mMyLiveList == null) {
            mMyLiveList = new ArrayList<>();
        }
        return mMyLiveList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mine_delete_collect_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public int getItemCount() {
        if(mMyLiveList.size()==0){
            Log.e("11111212", "11111111111"+mMyLiveList.size());
            EventBus.getDefault().post(new NullCollectEntity());
        }
        return mMyLiveList.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final MyLiveList myLive = mMyLiveList.get(holder.getAdapterPosition());
        holder.mTvRcmdTitle.setText(myLive.getFreeItemEntity().getCategory());
        holder.mTvRcmdFree.setText(myLive.getFreeItemEntity().getName());
        if (mEditMode == MYLIVE_MODE_CHECK) {
            holder.mIvSelectCollect.setVisibility(View.GONE);
        } else {
            holder.mIvSelectCollect.setVisibility(View.VISIBLE);

            if (myLive.isSelect()) {
                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_delete);
            } else {
                holder.mIvSelectCollect.setImageResource(R.mipmap.icon_collect_no_delete);
            }
        }
        holder.mTvRcmdTitle.setText((myLive.getFreeItemEntity().getName()));
        holder.mTvRcmdFree.setText(myLive.getFreeItemEntity().getVip_name());
        holder.mTvRcmdTime.setText("训练周期:"+myLive.getFreeItemEntity().getCycle()+"小时");
        holder.mTvRcmdNum.setText(myLive.getFreeItemEntity().getLearning_count()+"人学过");
        holder.ratingbarId.setCountSelected(myLive.getFreeItemEntity().getDifficulty());
        Glide.with(context).load(myLive.getFreeItemEntity().getCover()).asBitmap().placeholder(R.mipmap.init_logo).into(holder.mBgIvRcmd);
        holder.mBgIvRcmd.setAlpha(0.7f);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(holder.getAdapterPosition(), mMyLiveList);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mOnItemLongClickListener.onItemLongClickListener(holder.getAdapterPosition(), mMyLiveList);
                return true;
            }
        });
        holder.mIvSelectCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new SelectCallBack(holder.getAdapterPosition(), mMyLiveList));
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClickListener(int pos, List<MyLiveList> myLiveList);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.mOnItemLongClickListener = onItemLongClickListener;
    }
    public interface OnItemLongClickListener {
        void onItemLongClickListener(int pos, List<MyLiveList> myLiveList);
    }



    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.bg_iv_rcmd)
        ImageView mBgIvRcmd;
        @Bind(R.id.tv_rcmd_title)
        TextView mTvRcmdTitle;
        @Bind(R.id.tv_rcmd_free)
        TextView mTvRcmdFree;
        @Bind(R.id.tv_rcmd_time)
        TextView mTvRcmdTime;
        @Bind(R.id.tv_rcmd_num)
        TextView mTvRcmdNum;
        @Bind(R.id.ratingbarId)
        XLHRatingBar ratingbarId;
        @Bind(R.id.iv_select_collect)
        ImageView mIvSelectCollect;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
